package com.lhf.fvsooa.domain;

import com.lhf.userserviceapi.dto.FvsSysUserDTO;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p></p>
 *
 * @author liuhf
 * @createTime 2021/4/1 10:04
 * @since 1.0
 */
public class User implements UserDetails {
    private static final long serialVersionUID = 6455462058766271182L;
    //主键
    private Long uid;
    //用户名
    private String username;
    //密码
    private String password;
    //真实名字
    private String realName;
    //手机号
    private String phone;
    //邮箱
    private String email;
    //头像地址
    private String headImg;
    //是否删除
    private Integer isDel;
    //是否锁定
    private Integer isLock;

    private Set<SimpleGrantedAuthority> grantedAuthorities;


    public User() {
    }

    public User(FvsSysUserDTO dto) {
        this.uid = dto.getUid();
        this.username = dto.getUsername();
        this.password = dto.getPassword();
        this.realName = dto.getRealName();
        this.phone = dto.getPhone();
        this.email = dto.getEmail();
        this.headImg = dto.getHeadImg();
        this.isDel = dto.getIsDel();
        this.isLock = dto.getIsLock();
        this.grantedAuthorities = dto.getRoles().stream().map(SimpleGrantedAuthority::new).collect(Collectors.toSet());
    }


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return this.password;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.isLock == 0;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    public Long getUid() {
        return uid;
    }

    public void setUid(Long uid) {
        this.uid = uid;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getHeadImg() {
        return headImg;
    }

    public void setHeadImg(String headImg) {
        this.headImg = headImg;
    }

    public Integer getIsDel() {
        return isDel;
    }

    public void setIsDel(Integer isDel) {
        this.isDel = isDel;
    }

    public Integer getIsLock() {
        return isLock;
    }

    public void setIsLock(Integer isLock) {
        this.isLock = isLock;
    }
}
